//
//  LCSetAddressAreaView.m
//  lechuang
//
//  Created by Mr_hi on 2023/4/7.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCSetAddressAreaView.h"

typedef void(^doneBlock)(NSDictionary * dic);

@interface LCSetAddressAreaView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (strong, nonatomic)NSArray * dataArr;

@property (assign, nonatomic)NSInteger provinceRow;

@property (assign, nonatomic)NSInteger cityRow;

@property (copy, nonatomic)NSString * province;
@property (copy, nonatomic)NSString * provinceCode;
@property (copy, nonatomic)NSString * city;
@property (copy, nonatomic)NSString * cityCode;
@property (copy, nonatomic)NSString * area;
@property (copy, nonatomic)NSString * areaCode;

@property (nonatomic,assign)LCAddressStyle style;

@property (nonatomic,strong)doneBlock doneBlock;

@property (weak, nonatomic) IBOutlet UIView *alertView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;

@property (weak, nonatomic) IBOutlet UIPickerView *addressPicker;

@end


@implementation LCSetAddressAreaView

-(instancetype)initWithStyle:(LCAddressStyle)style Province:(NSString *)province City:(NSString *)city Block:(void(^)(NSDictionary * dic))block {
    self = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil][0];
    if (self) {
        self.style = style;
        [self getDataWithProvince:province City:city];
        [self configUI];
        if (block) {
            self.doneBlock = block;
        }
    }
    return self;
}

-(void)getDataWithProvince:(NSString *)province City:(NSString *)city {
    NSString * path = [[NSBundle mainBundle] pathForResource:@"adress" ofType:@"plist"];
    self.dataArr = [NSArray arrayWithContentsOfFile:path];
    
    switch (self.style) {
        case LCAddressStyleProvince: {
            self.province = @"北京市";
            self.provinceCode = @"110000";
            self.city = @"";
            self.cityCode =@"";
            self.area = @"";
            self.areaCode = @"";
        } break;
        case LCAddressStyleProvinceAndCity: {
            self.province = @"北京市";
            self.provinceCode = @"110000";
            self.city = @"北京城区";
            self.cityCode =@"110100";
            self.area = @"";
            self.areaCode = @"";
        } break;
        case LCAddressStyleProvinceCityArea: {
            self.province = @"北京市";
            self.provinceCode = @"110000";
            self.city = @"北京城区";
            self.cityCode =@"110100";
            self.area = @"东城区";
            self.areaCode = @"110101";
        } break;
        case LCAddressStyleCity: {
            self.province = @"";
            self.provinceCode = @"";
            self.city = @"";
            self.cityCode =@"";
            self.area = @"";
            self.areaCode = @"";
        } break;
        case LCAddressStyleArea: {
            self.province = @"";
            self.provinceCode = @"";
            self.city = @"";
            self.cityCode =@"";
            self.area = @"";
            self.areaCode = @"";
        } break;
    }
    self.provinceRow = 0;
    self.cityRow = 0;
    
    if (province.length) {
        for (int i = 0; i < self.dataArr.count; i++) {
            NSDictionary * dic = self.dataArr[i];
            NSString * provinceName = dic[@"name"];
            if ([provinceName containsString:province]) {
                self.province = provinceName;
                self.provinceRow = i;
                self.provinceCode = dic[@"code"];
                [self.addressPicker selectRow:self.provinceRow inComponent:0 animated:NO];
                NSArray * cityArr = dic[@"cities"];
                if (cityArr.count) {
                    NSDictionary * cityDic = [cityArr firstObject];
                    self.city = cityDic[@"name"];
                    self.cityCode = cityDic[@"code"];
                }
                return;
            }
        }
    }
    
    if (city.length) {
        for (int i = 0; i < self.dataArr.count; i++) {
            NSDictionary * dic = self.dataArr[i];
            NSArray * cityArr = dic[@"cities"];
            for (int j = 0; j< cityArr.count; j++) {
                NSDictionary * cityDic = cityArr[j];
                NSString * cityName = cityDic[@"name"];
                if ([cityName containsString:city]) {
                    self.province = dic[@"name"];
                    self.provinceCode = dic[@"code"];
                    self.provinceRow = i;
                    self.city = cityDic[@"name"];
                    self.cityCode = cityDic[@"code"];
                    self.cityRow = j;
                    [self.addressPicker selectRow:self.cityRow inComponent:1 animated:NO];
                    NSArray * areaArr = cityDic[@"areas"];
                    if (areaArr.count) {
                        NSDictionary * areaDic = [areaArr firstObject];
                        self.area = areaDic[@"name"];
                        self.areaCode = areaDic[@"code"];
                    }
                    return;
                }
            }
        }
    }
}

-(void)configUI {
    self.frame = CGRectMake(0, 0, ScreenW, ScreenH);
    [self whenTapped:^{
        [self dismiss];
    }];
    self.bottomConstraint.constant = -self.height;
    self.alpha = 0;
    [self layoutIfNeeded];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
}


-(void)show {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.25f animations:^{
        self.bottomConstraint.constant = 0;
        self.alpha = 1;
        [self layoutIfNeeded];
    }];
}

-(void)dismiss {
    [UIView animateWithDuration:0.25f animations:^{
        self.bottomConstraint.constant = -self.height;
        self.alpha = 0;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self removeFromSuperview];
    }];
}

- (IBAction)cancleAction:(UIButton *)sender {
    [self dismiss];
}

- (IBAction)doneAction:(UIButton *)btn {
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"province"] = self.province;
    dic[@"provinceCode"] = self.provinceCode;
    switch (self.style) {
        case LCAddressStyleProvinceCityArea:
        case LCAddressStyleArea: {
            dic[@"city"] = self.city;
            dic[@"cityCode"] = self.cityCode;
            dic[@"area"] = self.area;
            dic[@"areaCode"] = self.areaCode;
        } break;
        
        case LCAddressStyleProvinceAndCity:
        case LCAddressStyleCity: {
            dic[@"city"] = self.city;
            dic[@"cityCode"] = self.cityCode;
        } break;
        case LCAddressStyleProvince: {
        } break;
    }
    self.doneBlock(dic);
    [self dismiss];
}

#pragma mark - UIPickerViewDelegate,UIPickerViewDataSource
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    switch (self.style) {
        case LCAddressStyleProvinceCityArea: {
            return 3;
        } break;
        case LCAddressStyleProvinceAndCity: {
            return 2;
        } break;
        default: {
            return 1;
        } break;
    }
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    switch (self.style) {
        case LCAddressStyleProvinceCityArea: {
            if (component == 0) {
                return self.dataArr.count;
            } else if (component == 1) {
                NSDictionary * dic = self.dataArr[self.provinceRow];
                NSArray * cityArr = dic[@"cities"];
                if (cityArr.count) {
                    return cityArr.count;
                }
                return 0;
            } else {
                NSDictionary * dic = self.dataArr[self.provinceRow];
                NSArray * cityArr = dic[@"cities"];
                if (cityArr.count) {
                    NSDictionary * cityDic = cityArr[self.cityRow];
                    NSArray * areaArr = cityDic[@"areas"];
                    if (areaArr.count) {
                        return areaArr.count;
                    }
                }
                return 0;
            }
        } break;
        case LCAddressStyleProvinceAndCity: {
            if (component == 0) {
                return self.dataArr.count;
            } else if (component == 1) {
                NSDictionary * dic = self.dataArr[self.provinceRow];
                NSArray * cityArr = dic[@"cities"];
                if (cityArr.count) {
                    return cityArr.count;
                }
                return 0;
            }
        } break;
        case LCAddressStyleProvince: {
            return self.dataArr.count;
        } break;
        case LCAddressStyleCity: {
            NSDictionary * dic = self.dataArr[self.provinceRow];
            NSArray * cityArr = dic[@"cities"];
            if (cityArr.count) {
                return cityArr.count;
            }
            return 0;
        } break;
        case LCAddressStyleArea: {
            NSDictionary * dic = self.dataArr[self.provinceRow];
            NSArray * cityArr = dic[@"cities"];
            if (cityArr.count) {
                NSDictionary * cityDic = cityArr[self.cityRow];
                NSArray * areaArr = cityDic[@"areas"];
                if (areaArr.count) {
                    return areaArr.count;
                }
            }
            return 0;
        } break;
    }
    return 0;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *myView = nil;
    myView = [[UILabel alloc] init];
    myView.textAlignment = NSTextAlignmentCenter;
    myView.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    myView.font = [UIFont systemFontOfSize:14];         //用label来设置字体大小
    myView.numberOfLines = 2;
    myView.backgroundColor = [UIColor clearColor];
    return myView;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    switch (self.style) {
        case LCAddressStyleProvinceCityArea: {
            if (component == 0) {
                NSDictionary * dic = self.dataArr[row];
                return dic[@"name"];
            } else if (component == 1) {
                NSDictionary * dic = self.dataArr[self.provinceRow];
                NSArray * cityArr = dic[@"cities"];
                if (cityArr.count) {
                    NSDictionary * cityDic = cityArr[row];
                    return cityDic[@"name"];
                }
                return @"";
            } else {
                NSDictionary * dic = self.dataArr[self.provinceRow];
                NSArray * cityArr = dic[@"cities"];
                if (cityArr.count) {
                    NSDictionary * cityDic = cityArr[self.cityRow];
                    NSArray * areaArr = cityDic[@"areas"];
                    if (areaArr.count) {
                        NSDictionary * areaDic = areaArr[row];
                        return areaDic[@"name"];
                    }
                }
                return @"";
            }
        } break;
        case LCAddressStyleProvinceAndCity: {
            if (component == 0) {
                NSDictionary * dic = self.dataArr[row];
                return dic[@"name"];
            } else {
                NSDictionary * dic = self.dataArr[self.provinceRow];
                NSArray * cityArr = dic[@"cities"];
                if (cityArr.count) {
                    NSDictionary * cityDic = cityArr[row];
                    return cityDic[@"name"];
                }
                return @"";
            }
        } break;
            
        case LCAddressStyleProvince: {
            NSDictionary * dic = self.dataArr[row];
            return dic[@"name"];
        } break;
        case LCAddressStyleCity: {
            NSDictionary * dic = self.dataArr[self.provinceRow];
            NSArray * cityArr = dic[@"cities"];
            if (cityArr.count) {
                NSDictionary * cityDic = cityArr[row];
                return cityDic[@"name"];
            }
            return @"";
        } break;
        case LCAddressStyleArea: {
            NSDictionary * dic = self.dataArr[self.provinceRow];
            NSArray * cityArr = dic[@"cities"];
            if (cityArr.count) {
                NSDictionary * cityDic = cityArr[self.cityRow];
                NSArray * areaArr = cityDic[@"areas"];
                if (areaArr.count) {
                    NSDictionary * areaDic = areaArr[row];
                    return areaDic[@"name"];
                }
            }
            return @"";
        } break;
    }
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 40;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    switch (self.style) {
        case LCAddressStyleProvinceCityArea: {
            if (component == 0) {
                self.provinceRow = row;
                self.cityRow = 0;
                [pickerView reloadComponent:1];
                [pickerView reloadComponent:2];
                [pickerView selectRow:0 inComponent:1 animated:YES];
                [pickerView selectRow:0 inComponent:2 animated:YES];
                
                NSDictionary * dic = self.dataArr[self.provinceRow];
                self.province = dic[@"name"];
                self.provinceCode = dic[@"code"];
                NSArray * cityArr = dic[@"cities"];
                if (cityArr.count) {
                    NSDictionary * cityDic = cityArr[self.cityRow];
                    self.city = cityDic[@"name"];
                    self.cityCode = cityDic[@"code"];
                    NSArray * areaArr = cityDic[@"areas"];
                    if (areaArr.count) {
                        NSDictionary * areaDic = [areaArr firstObject];
                        self.area = areaDic[@"name"];
                        self.areaCode = areaDic[@"code"];
                    } else {
                        self.area = @"";
                        self.areaCode = @"";
                    }
                } else {
                    self.city = @"";
                    self.cityCode = @"";
                    self.area = @"";
                    self.areaCode = @"";
                }
            } else if (component == 1) {
                self.cityRow = row;
                [pickerView reloadComponent:2];
                [pickerView selectRow:0 inComponent:2 animated:YES];
                
                NSDictionary * dic = self.dataArr[self.provinceRow];
                NSArray * cityArr = dic[@"cities"];
                
                if (cityArr.count) {
                    NSDictionary * cityDic = cityArr[self.cityRow];
                    self.city = cityDic[@"name"];
                    self.cityCode = cityDic[@"code"];
                    NSArray * areaArr = cityDic[@"areas"];
                    if (areaArr.count) {
                        NSDictionary * areaDic = [areaArr firstObject];
                        self.area = areaDic[@"name"];
                        self.areaCode = areaDic[@"code"];
                    } else {
                        self.area = @"";
                        self.areaCode = @"";
                    }
                } else {
                    self.city = @"";
                    self.cityCode = @"";
                    self.area = @"";
                    self.areaCode = @"";
                }
            } else {
                NSDictionary * dic = self.dataArr[self.provinceRow];
                NSArray * cityArr = dic[@"cities"];
                if (cityArr.count) {
                    NSDictionary * cityDic = cityArr[self.cityRow];
                    NSArray * areaArr = cityDic[@"areas"];
                    if (areaArr.count) {
                        NSDictionary * areaDic = areaArr[row];
                        self.area = areaDic[@"name"];
                        self.areaCode = areaDic[@"code"];
                    } else {
                        self.area = @"";
                        self.areaCode = @"";
                    }
                } else {
                    self.area = @"";
                    self.areaCode = @"";
                }
            }
        } break;
        case LCAddressStyleProvinceAndCity: {
            if (component == 0) {
                self.provinceRow = row;
                [pickerView reloadComponent:1];
                [pickerView selectRow:0 inComponent:1 animated:YES];
                
                NSDictionary * dic = self.dataArr[self.provinceRow];
                self.province = dic[@"name"];
                self.provinceCode = dic[@"code"];
                NSArray * cityArr = dic[@"cities"];
                if (cityArr.count) {
                    NSDictionary * cityDic = [cityArr firstObject];
                    self.city = cityDic[@"name"];
                    self.cityCode = cityDic[@"code"];
                } else {
                    self.city = @"";
                    self.cityCode = @"";
                }
            } else {
                NSDictionary * dic = self.dataArr[self.provinceRow];
                NSArray * cityArr = dic[@"cities"];
                NSDictionary * cityDic = cityArr[row];
                self.city = cityDic[@"name"];
                self.cityCode = cityDic[@"code"];
            }
        } break;
        case LCAddressStyleProvince: {
            NSDictionary * dic = self.dataArr[row];
            self.province = dic[@"name"];
            self.provinceCode = dic[@"code"];
        } break;
        case LCAddressStyleCity: {
            NSDictionary * dic = self.dataArr[self.provinceRow];
            NSArray * cityArr = dic[@"cities"];
            if (cityArr.count) {
                NSDictionary * cityDic = cityArr[row];
                self.city = cityDic[@"name"];
                self.cityCode = cityDic[@"code"];
            } else {
                self.city = @"";
                self.cityCode = @"";
            }
        } break;
        case LCAddressStyleArea: {
            NSDictionary * dic = self.dataArr[self.provinceRow];
            NSArray * cityArr = dic[@"cities"];
            NSDictionary * cityDic = cityArr[self.cityRow];
            NSArray * areaArr = cityDic[@"areas"];
            if (areaArr.count) {
                NSDictionary * areaDic = areaArr[row];
                self.area = areaDic[@"name"];
                self.areaCode = areaDic[@"code"];
            } else {
                self.area = @"";
                self.areaCode = @"";
            }
        } break;
    }
}

@end
